const http = require("http")
const fs = require("fs")
const qs = require("querystring")
const server = http.createServer((req, res) => {

  if (req.url == "/upload") {
    if (req.method == "POST") {
      // const fileWrite = fs.createWriteStream("./foo.png", { flags: "a+" })
      let body = ""
      res.on("data", (data) => {
        body += data
        // fileWrite.write(data)
      })
      res.on("end", () => {
        console.log(body);

        const payload = qs.parse(body, '\r\n', ":")
        const type = payload["Content-Type"]
        const typeIndex = body.indexOf(type)
        const typeLength = type.length

        console.log(type);

        // body.substring
        res.end("end")
      })
    }
  }

})

server.listen(8001, () => {
  console.log("服务器开启成功");
})